$(function () {
	//触摸点距离盒子的距离
	var box_x,box_y;
	//计时器,计数器
	var timerCounter,guostCounter;
	//全局计数变量
	var globalNum = 0;
	//捉多少鬼算成功
	var totalGuost = 2;
	//创建计时器
	var timer,moveTimer;
	//次数限制
	var limitNum = 3;
	//游戏区域的高度
	var gameAreaHeight = $('#guostArea').height();

	//点击[开始捉鬼]按钮
	$('#startBtn').on('click',function () {
		$('#startPage').hide();
		$('#gameTips').show();
	});

	//点击[知道了]按钮
	$('#iKnowBtn').on('click',function () {
		if (limitNum <= 0) {
			alert('游戏次数超过限制,请联系管理员');
			return;
		};
		$('#gameTips').hide();
		$('#gamePage').show();
		gameBegin();
	});

	//点击[再捉一次]按钮
	$('.tryAgain').on('click',function () {
		if (limitNum <= 0) {
			alert('游戏次数超过限制,请联系管理员');
			return;
		};
		$('#sucessPage,#failPage').hide();
		$('#gamePage').show();
		gameBegin();
	});

	//给鬼绑定拖动事件
	$('#guostArea').on('touchstart','.person',function(e){
		e.preventDefault();
		var elm = $(this).position();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		box_x = touch.pageX - elm.left;
		box_y = touch.pageY - elm.top;
	});
	$('#guostArea').on('touchmove','.person',function(e){
		e.preventDefault();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		$(this).css('left', touch.pageX - box_x + 'px');
		$(this).css('top', touch.pageY - box_y + 'px'); 
	});
	$('#guostArea').on('touchend','.person',function(e){
		e.preventDefault();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		var elm = $(this).position();
		var top = elm.top;
		if (top => gameAreaHeight) {
			$(this).removeClass('person').addClass('personIn');
			$('#counter').text(++guostCounter);
		}
	});

	//游戏开始
	function gameBegin() {
		//次数减少一次
		limitNum--;
		//初始化
		timerCounter = 3;
		guostCounter = 0;
		globalNum = 0;
		$('#timeCounter').text(timerCounter);
		$('#counter').text(guostCounter);
		$('#guostArea').html('');
		//倒计时
		timer = setInterval(function () {
			$('#timeCounter').text(--timerCounter);
			if (timerCounter === 0) {
				clearInterval(timer);
				clearInterval(moveTimer);
				if (guostCounter >= totalGuost) {
					$('#sucessPage').show();
					$('#sucessNum').text(guostCounter);
				} else {
					$('#failPage').show();
					$('#failNum').text(guostCounter);
					$('#differNum').text(totalGuost - guostCounter);
				};
			};
		}, 1000);
		//移动事件
		guostMove();
		moveTimer = setInterval(guostMove,1000/60);
	};

	//鬼的移动事件
	function guostMove() {
		//定时生成鬼
		if (globalNum%60 === 0) {
			createGuost();
		};
		var guosts = $('.person');
		guosts.map(function (ele, i) {
			var old = i.offsetLeft;
			if (old < -100) {
				$(this).remove();
			} else {
				i.style.left = old - 2 + 'px';
			};
		});
		globalNum++;
	};

	//随机生成鬼
	function createGuost() {
		//父盒子高度7.5rem 鬼高度2.2rem;
		//top值随机范围 0 - 5.3rem;
		//产生随机数
		var num = (Math.random()*5.3).toFixed(1);
		var typeRan = Math.random();
		var personType = typeRan<0.33?'1':typeRan<0.66?'2':'3';
		var html = '<div class="person" data-type="'+personType+'" style="left:8rem;top:'+num+'rem"></div>';
		$('#guostArea').append(html);
	};

});


